package com.iap.admin.view.sample.code
{
	import com.iap.common.event.SampleFillerEvent;
	import com.iap.common.signal.SampleRequestSignal;
	import com.iap.admin.view.code.EditViewCode;
	import com.iap.admin.view.sample.SampleEditView;
	import com.iap.admin.view.sample.SampleFillerRenderer;
	import com.iap.admin.view.sample.SampleFillerSelectorWindow;
	import com.iap.app.util.FormUtils;
	import com.iap.app.util.Utils;
	import com.iap.common.model.Filler;
	import com.iap.common.model.Sample;
	import com.iap.common.model.SampleFiller;
	import com.iap.common.model.VectorModel;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.core.IFlexDisplayObject;
	import mx.core.UIComponent;
	import mx.events.CloseEvent;
	import mx.events.ValidationResultEvent;
	import mx.managers.PopUpManager;
	import mx.utils.ArrayUtil;
	import mx.validators.StringValidator;
	import mx.validators.Validator;
	
	import org.osflash.signals.Signal;
	
	import spark.components.Group;
	import spark.components.supportClasses.SkinnableComponent;
	import spark.events.IndexChangeEvent;
	
	public class SampleEditViewCode extends EditViewCode
	{
		public static const CANCEL_NAME:String = "SampleCreate.Cancel";
		public static const SAVE_NAME:String = "SampleCreate.Save";
		public static const DELETE_NAME:String = "SampleCreate.Delete";

		public var onSubmit:SampleRequestSignal = new SampleRequestSignal();

		private var nameValidator:StringValidator;
		private var view:SampleEditView;
		public var fillers:ArrayCollection;
		
		[Bindable]
		protected var sampleFillers:ArrayCollection;

		public function SampleEditViewCode()
		{
			super();
		}
		
		private function get sampleModel():Sample
		{
			return model as Sample;
		}
		private function set sampleModel(model:Sample):void
		{
			this.model = model;
		}

		protected function initView(view:SampleEditView):void
		{
			this.view = view;
			addListener();
			initValidators();
			resetFocus();
			onReady.dispatch("ready");
		}
		
		private function initValidators():void
		{
			nameValidator = new StringValidator();
			
			nameValidator.source = view.nameForm;
			nameValidator.property = "text";
			nameValidator.required = true;
			nameValidator.maxLength = 100;
			
			validateData.push(nameValidator);
		}

		private function addListener():void
		{
			view.nameForm.addEventListener(Event.CHANGE, validateForm);
			view.titleForm.addEventListener(Event.CHANGE, validateForm);
			view.sampleFillerControl.addEventListener(SampleFillerEvent.CHANGE, changeFillerHandler);
		}

		private function removeListener():void
		{
			view.nameForm.removeEventListener(Event.CHANGE, validateForm);
			view.titleForm.removeEventListener(Event.CHANGE, validateForm);
			view.sampleFillerControl.removeEventListener(SampleFillerEvent.CHANGE, changeFillerHandler);
		}

		private function resetFocus():void
		{
			view.focusManager.setFocus(view.nameForm);
		}

		protected function submit():void 
		{
			model ||= new Sample();
			model = FormUtils.fillObjectFromControls(view, model, FORM_PREFIX);
			sampleModel.sampleFillers = getValidSampleFillers();
			onSubmit.dispatch(model, SampleRequestSignal.SAVE);
		}
		
		private function getValidSampleFillers():Array
		{
			var list:Array = new Array();
			for each(var item:SampleFiller in sampleFillers)
			{
				if(item.filler != null)
				{
					list.push(item);
				}
			}
			return list;
		}

		protected function remove():void 
		{
			onSubmit.dispatch(model, SampleRequestSignal.DELETE);
		}

		protected function vectorModelForm_changeHandler(evt:IndexChangeEvent):void
		{
			var vectorModel:VectorModel = view.vectorModelForm.dataProvider[evt.newIndex] as VectorModel;
			setVectorModel(vectorModel);
		}
		
		public function updatePreviewFromModel():void
		{
			var item:VectorModel = sampleModel.vectorModel;
			setSampleFiller(item.vectorImages.length);
			view.preview.setSource(item.vectorImages, sampleFillers.toArray());
		}
		
		private function setSampleFiller(layerCount:int):void
		{
			var list:ArrayCollection = new ArrayCollection(sampleModel.sampleFillers.slice(0, layerCount));
			list ||= new ArrayCollection();
			
			var item:SampleFiller;
			for(var i:uint = 1; i<layerCount+1; i++)
			{
				if(i>list.length)
				{
					item = new SampleFiller();
					item.sampleId = sampleModel.id;
					item.layer = i;
					list.addItem(item);
				}
			}
			sampleFillers = list;
		}
		
		private function setVectorModel(vectorModel:VectorModel):void
		{
			if(sampleModel == null) return;
			sampleModel.vectorModel = vectorModel;
			updatePreviewFromModel();
		}

		private function changeFillerHandler(evt:SampleFillerEvent):void
		{
			var popup:SampleFillerSelectorWindow = new SampleFillerSelectorWindow();
			popup.fillers = fillers;
			popup.sampleFiller = evt.model;
			PopUpManager.addPopUp(popup, view, true);
			PopUpManager.centerPopUp(popup);
			popup.saveSignal.add(changeFillerSaveHandler);
			popup.addEventListener(CloseEvent.CLOSE, changeFillerCloseHandler);
		}
		
		private function changeFillerCloseHandler(evt:CloseEvent):void
		{
			PopUpManager.removePopUp(evt.currentTarget as IFlexDisplayObject);
		}

		private function changeFillerSaveHandler(item:SampleFiller, target:SampleFillerSelectorWindow):void
		{
			for(var i:uint = 0; i<sampleFillers.length; i++)
			{
				var sampleFiller:SampleFiller = sampleFillers[i] as SampleFiller;
				if(sampleFiller.layer == item.layer)
				{
					sampleFillers[i] = item;
				}
			}
			PopUpManager.removePopUp(target as IFlexDisplayObject);
			view.preview.setSampleFiller(item);
		}
	}
}